package skipClass.java;


import java.io.IOException;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.audio.music.Music;
import org.anddev.andengine.audio.music.MusicFactory;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.TextMenuItem;
import org.anddev.andengine.entity.scene.menu.item.decorator.ColorMenuItemDecorator;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Debug;
import org.anddev.andengine.entity.scene.Scene.ITouchArea;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.input.touch.TouchEvent;

//import android.Anim.R;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.KeyEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;



public class Skippers extends BaseGameActivity implements IOnMenuItemClickListener {

	// ===========================================================
    // Constants
    // ===========================================================

    private static final int CAMERA_WIDTH = 480;
    private static final int CAMERA_HEIGHT = 800;
    private static final int LAYER_BACKGROUND = 0;
    private static final int LAYER_SCORE = LAYER_BACKGROUND + 1;
    private static final int FONT_SIZE = 48;
    protected static final int MENU_RESET = 0;
    protected static final int MENU_QUIT = MENU_RESET + 1;

    // ===========================================================
    // Fields
    // ===========================================================

    private Camera mCamera;


    /*private Font mDroidFont;
    private Font mPlokFont;
    private Font mNeverwinterNightsFont;
    private Font mUnrealTournamenFont;
    private Font mKingdomOfHeartsFont;

    private Texture mDroidFontTexture;
    private Texture mPlokFontTexture;
    private Texture mNeverwinterNightsFontTexture;
    private Texture mUnrealTournamentFontTexture;
    private Texture mKingdomOfHeartsFontTexture;*/
    private Font HandFont;
    private Texture HandTexture;
    private Texture paperTexture;
    private TextureRegion paperRegion;
    private Music mMusic;
    protected Scene mMainScene;
    private MenuScene mMenuScene;
    private ChangeableText mScoreText;
	private Texture mOnScreenMuteTexture;
	private Texture NoteTexture;
	private TextureRegion NoteRegion;
	private TextureRegion mOnScreenMuteRegion;
    protected TextureRegion mMenuResetTextureRegion;
    protected TextureRegion mMenuQuitTextureRegion;
    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    @Override
    public Engine onLoadEngine() {
            this.mCamera = new Camera(100, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
            return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera).setNeedsMusic(true));
    }

    @Override
    public void onLoadResources() {
           
    	TextureRegionFactory.setAssetBasePath("bg/");
		this.paperTexture = new Texture(1024, 1024, TextureOptions.DEFAULT);
		this.paperRegion = TextureRegionFactory.createFromAsset(this.paperTexture, this, "paper.jpg", 0, 0);
		this.mOnScreenMuteTexture = new Texture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mOnScreenMuteRegion = TextureRegionFactory.createFromAsset(this.mOnScreenMuteTexture, this, "mute.png", 0, 0);
		this.NoteTexture = new Texture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.NoteRegion = TextureRegionFactory.createFromAsset(this.NoteTexture, this, "note.png", 0, 0);

    	this.HandTexture = new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    	FontFactory.setAssetBasePath("font/");
    	this.HandFont = FontFactory.createFromAsset(this.HandTexture, this, "GoodDog.otf", FONT_SIZE, true, Color.BLACK);
    	this.mEngine.getFontManager().loadFonts(this.HandFont);
    
    	TextureRegionFactory.setAssetBasePath("drawable/");
    	//this.topfaceTextureRegion = TextureRegionFactory.createTiledFromAsset(this.paperTexture,this,"topfullsmall.png", 200,75,2,2);

		

        
    	this.mEngine.getTextureManager().loadTextures( this.NoteTexture,this.mOnScreenMuteTexture,this.HandTexture,this.paperTexture);
        MusicFactory.setAssetBasePath("mfx/");
        try {
                this.mMusic = MusicFactory.createMusicFromAsset(this.mEngine.getMusicManager(), this, "The Lazy Song.mp3");
                this.mMusic.setLooping(true);
        } catch (final IOException e) {
                Debug.e(e);
        }
    	
    }

    @Override
    public Scene onLoadScene() {
    	
    		this.mMenuScene = this.createMenuScene();
    		//paper background
            this.mEngine.registerUpdateHandler(new FPSLogger());

            this.mMainScene = new Scene(2);
            this.mMainScene.setBackgroundEnabled(false);
            this.mMainScene.getChild(LAYER_BACKGROUND).attachChild(new Sprite(0, 0, this.paperRegion));
            //this.mMusic.play();
            
         
            
            int x = (CAMERA_WIDTH - this.mOnScreenMuteRegion.getWidth())/4;
            int y = (CAMERA_HEIGHT - this.mOnScreenMuteRegion.getHeight()) ;
            final Sprite mute = new Sprite(x, y, this.mOnScreenMuteRegion);
            x = 450 ;
            y = 680 ;
            final Sprite note = new Sprite(x, y, this.NoteRegion);
            
            final IEntity lastChild = this.mMainScene.getLastChild();
            lastChild.attachChild(new Text(200, 30, this.HandFont,"SKIP CLASS & PASS"));
            lastChild.attachChild(new Text(130, 100, this.HandFont,"Score: 0"));
            lastChild.attachChild(new Text(400, 100, this.HandFont,"Excuses: 3"));
            lastChild.attachChild(new Text(200,600, this.HandFont,"Click Menu To Begin"));
            
            mMainScene.getLastChild().attachChild(note);
            mMainScene.getLastChild().attachChild(mute);
            mMainScene.registerTouchArea(mute);mMainScene.getLastChild().attachChild(mute);
            
            mMainScene.setOnAreaTouchListener(new IOnAreaTouchListener() {

				@Override
				public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
						ITouchArea pTouchArea, float pTouchAreaLocalX,
						float pTouchAreaLocalY) {
					if(pSceneTouchEvent.isActionDown()) {
						if(Skippers.this.mMusic.isPlaying()){
							Skippers.this.mMusic.pause();}
						else{Skippers.this.mMusic.play();}
                }
                return true;
				}
            });
           
           

            return this.mMainScene;
    }

    @Override
    public void onLoadComplete() {

    }

    @Override
    public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
    		
            if(pKeyCode == KeyEvent.KEYCODE_MENU && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
                    if(this.mMainScene.hasChildScene()) {
                            /* Remove the menu and reset it. */
                    		this.mMusic.play();
                            this.mMenuScene.back();
                    } else {
                            /* Attach the menu. */
                            this.mMainScene.setChildScene(this.mMenuScene, false, true, true);
                    }
                    return true;
            } else {
                    return super.onKeyDown(pKeyCode, pEvent);
            }
    }

    @Override
    public boolean onMenuItemClicked(final MenuScene pMenuScene, final IMenuItem pMenuItem, final float pMenuItemLocalX, final float pMenuItemLocalY) {
    	
    	switch(pMenuItem.getID()) {
                    case MENU_RESET:
                    	Context context = this.getBaseContext();
                    	Intent explicitIntent = new Intent(context,skipClass.java.QuestionListActivity.class);
                    	explicitIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    	context.startActivity(explicitIntent);
                            return true;
                    case MENU_QUIT:
                            /* End Activity. */
                            this.finish();
                            return true;
                    default:
                            return false;
            }
    }

    // ===========================================================
    // Methods
    // ===========================================================

    protected MenuScene createMenuScene() {
            final MenuScene menuScene = new MenuScene(this.mCamera);

            final IMenuItem resetMenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_RESET, this.HandFont, "Kick Back & Slack"), 1.0f,0.0f,0.0f, 0.0f,0.0f,0.0f);
            resetMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
            menuScene.addMenuItem(resetMenuItem);

            final IMenuItem quitMenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_QUIT, this.HandFont, "Just Go To Class (Quit)"), 1.0f,0.0f,0.0f, 0.0f,0.0f,0.0f);
            quitMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
            menuScene.addMenuItem(quitMenuItem);

            menuScene.buildAnimations();

            menuScene.setBackgroundEnabled(false);

            menuScene.setOnMenuItemClickListener(this);
            return menuScene;
    }

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}